package com.erp.module.jiaFang.addJiaFang

import android.content.Context
import android.util.Log
import com.erp.bean.AddJiaFangBean
import com.erp.bean.JiaFangDBean
import com.erp.mclass.UnCeHandler
import com.erp.Interface.OnUserDataInterface
import com.erp.util.Httputil
import com.erp.util.Httputil.getJiaFangDanJu
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import org.json.JSONException
import org.json.JSONObject

/**
 * Created by Administrator on 2018/6/28/028.
 * 作者：刘洋
 */
class IAddJiaFangKP(val KView: AddJiaFangKView, val context: Context, val mlist: MutableList<AddJiaFangBean>, val code : Int) : AddJiaFangKP {
    private var isdanju = false
    private val danjuBean = JiaFangDBean()
    override fun getDanJu(s: String) {
        UnCeHandler.buzhou = "getDanJu"
        getJiaFangDanJu(context,s,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
//                Log.d("TAAG",Myutil.decodeUnicode(data))
                if(data==null){
                    KView.getDanHaoNO("")
                }
                try {
                    val jo = JSONObject(data)
                    danjuBean.oid=JsonUtil.getvolue(jo,"oid")
                    danjuBean.o_onum=JsonUtil.getvolue(jo,"o_onum")
                    danjuBean.ot_name=JsonUtil.getvolue(jo,"ot_name")
                    danjuBean.shop_name=JsonUtil.getvolue(jo,"shop_name")
                    val js = JsonUtil.getjsvolue(jo,"material")
                    for(i in 0 until js.length()){
                        val o=js[i] as JSONObject
                        val bean = JiaFangDBean.AddJiaFangDBianMa()
                        bean.m_id=JsonUtil.getvolue(o,"m_id")
                        bean.m_counts=JsonUtil.getvolue(o,"m_counts")
                        bean.m_material_num=JsonUtil.getvolue(o,"m_material_num")
                        danjuBean.materials.add(bean)
                    }
                }catch (e : JSONException){
                    e.printStackTrace()
                }
                isdanju=true
                KView.getDanHaoOK(danjuBean)
            }

            override fun onNot(throwable: String?) {
//                Log.d("TAAG",Myutil.decodeUnicode(throwable))
                if(throwable==null){
                    KView.getDanHaoNO("获取数据失败")
                    return
                }
                if(throwable.indexOf("找不到")!=-1){
                    KView.getDanHaoNO("找不到生产单号")
                }else{
                    KView.getDanHaoNO("获取数据失败")
                }
            }

        })
    }

    override fun getBianMa(s: String) {
        UnCeHandler.buzhou = "getBianMa"
        var bianmaBean = JiaFangDBean.AddJiaFangDBianMa()
        if (code == 2) {
            if (!isdanju) {
                KView.getBianMaNo("请先输入单号再添加")
                return
            }
            var iscunzai = false
            for(bianma in danjuBean.materials){
                if(s==bianma.m_material_num){
                    bianmaBean=bianma
                    iscunzai=true
                    break
                }
            }
            if(!iscunzai){
                KView.getBianMaNo("该生产单中没有这个货品编码")
                return
            }
        }
        for (bean in mlist) {
            if (s == bean.code) {
                KView.getBianMaNo("该货品编码已经存在，不能重复添加")
                return
            }
        }

        Httputil.getJiaFangBianMa(context, s, object : OnUserDataInterface {
            override fun onUserData(data: String?) {
                if (data == null) {
                    return
                }
                val bean = AddJiaFangBean()
                Log.d("TAAG","data="+data)
                try {
                    var jo = JSONObject(data)
                    bean.code = JsonUtil.getvolue(jo, "code")
                    bean.id = JsonUtil.getvolue(jo, "id")
                    bean.name = JsonUtil.getvolue(jo, "name")
                    if(code==1){
                        bean.amount="0"
                    }else if(code==2){
                        bean.amount=bianmaBean.m_counts
                        bean.oid= danjuBean.oid
                    }
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                if (bean.code != "") {
                    var ischongdu = false
                    for (b in mlist) {
                        if (b.code == bean.code) {
                            ischongdu = true
                            break
                        }
                    }
                    if (ischongdu) {
                        KView.getBianMaNo("该货品编码已经存在，不能重复添加")
                    } else {
                        KView.getBianMaOk(bean)
                    }
                } else {
                    KView.getBianMaNo("没有找到该货品编码")
                }
            }

            override fun onNot(throwable: String?) {
//                Log.d("TAAG","throwable="+Myutil.decodeUnicode(throwable))
                KView.getBianMaNo("")
            }

        })
    }

    override fun addJiaFang(cangku : String, beizhu: String) {
        UnCeHandler.buzhou = "addJiaFang"
        var iw_id = "0"
        when(cangku){
            "专卖店用品仓" -> iw_id="6"
            "中间仓" -> iw_id="5"
            "五金仓" -> iw_id="3"
            "其它材料仓" -> iw_id="2"
            "华标三区仓" -> iw_id="1"
            "原料仓" -> iw_id="4"
            "饰品仓" -> iw_id="7"
        }
        var iioo_type = ""
        when(code){
            1 -> iioo_type="1"
            2 -> iioo_type="-1"
        }
        for(b in mlist){
            if((b.amount=="")||b.amount=="0"){
                Myutil.MyTost("货品数量不能为0")
                KView.addJiaFangNo()
                return
            }
        }
        Httputil.addJiaFang(context,mlist,iw_id,iioo_type,beizhu,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
                Log.d("TAAG","data="+Myutil.decodeUnicode(data))
                if(data==null){
                    KView.addJiaFangNo()
                    Myutil.MyTost("上传失败")
                }else{
                    Myutil.MyTost("上传成功")
                    KView.addJiaFangOk()
                }
            }

            override fun onNot(throwable: String?) {
                Myutil.MyTost("上传失败")
                Log.d("TAAG","throwable="+Myutil.decodeUnicode(throwable))
                KView.addJiaFangNo()
            }

        })
    }
}